package com.wxxymaker.controller;

import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
import com.wxxymaker.interceptor.annocation.AuthRequire;
import com.wxxymaker.model.entity.Admin;
import com.wxxymaker.model.entity.MarkRecord;
import com.wxxymaker.model.entity.User;
import com.wxxymaker.service.MarkRecordService;
import com.wxxymaker.service.MarkService;
import com.wxxymaker.service.StageService;
import com.wxxymaker.service.UserService;
import com.wxxymaker.utils.Const;
import com.wxxymaker.utils.SimpleCache;
import com.wxxymaker.utils.web.ResponseWarp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: Soft
 * @Date:   2018/5/9 18:24
 * @Desc:   打分记录控制器
 */
@RestController
@RequestMapping("/markRecord")
public class MarkRecordHandler {

    @Autowired
    private MarkService markService;

    @Autowired
    private MarkRecordService markRecordService;

    @Autowired
    private StageService stageService;

    @Autowired
    private UserService userService;

    /**
     * 漏洞：这里没有判断用户是否有打分权限，比如学生不能给教师食堂打分。!!!!!!
     *      前端有一层判断，在/user/markView，后端没有
     *
     * 添加一条打分记录,打分记录包括所有...内容,除了时间
     * @param markRecord 记录内容
     */
    @PostMapping("/add")
    public ResponseWarp add(MarkRecord markRecord, @RequestParam String limit, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        if (user != null) {
            markRecord.setUser(user);
            if (markService.isCan()) {
                String QRLimit = (String) SimpleCache.get(Const.QRLIMIT);
                if (null != QRLimit && QRLimit.equals(limit)) {
                    if (!markRecordService.isHas(markRecord.getUser(), markRecord.getShop(), markRecord.getStage())) {
                        MarkRecord record = markRecordService.add(markRecord);
                        if (null != record) {
                            return ResponseWarp.ok("打分成功!");
                        }
                    } else {
                        return ResponseWarp.error("已经打过分了！");
                    }
                } else {
                    return ResponseWarp.error("打分链接已失效！");
                }
            } else {
                return ResponseWarp.error("打分功能被关闭！");
            }
        } else {
            return ResponseWarp.error("没有用户权限！");
        }
        return ResponseWarp.error("打分操作失败!");
    }

    /**
     * 增加数量，阶段打分人数，用户参评数量
     * @param stageId 阶段id
     * @param limit   限制
     */
    @PostMapping("/addNum")
    public ResponseWarp addNum(@RequestParam Integer stageId, @RequestParam String limit,
                               HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        if (user != null) {
            String QRLimit = (String) SimpleCache.get(Const.QRLIMIT);
            if (null != QRLimit && QRLimit.equals(limit)) {
                // 增加打分阶段的打分人数，以及用户的参评次数
                stageService.addUserSum(stageId);
                userService.addNum(user.getId());
                return ResponseWarp.ok("计数操作成功!");
            } else {
                return ResponseWarp.error("链接已失效！");
            }
        } else {
            return ResponseWarp.error("没有用户权限！");
        }
    }

    /**
     * 根据id 删除打分记录 基本没用
     * @param id  要删除的记录id
     */
    @PostMapping("/delete/{id}")
    public ResponseWarp delete(@PathVariable Integer id, HttpServletRequest request) {
        Admin admin = (Admin) request.getAttribute("admin");
        if (admin.getGrade() > 1) {
            if (markRecordService.delete(id) != null) {
                return ResponseWarp.ok("删除成功!");
            }
        }
        return ResponseWarp.error("删除失败！没有权限！");
    }

    /**
     * 根据id获取一条记录 没啥用
     * @param id  记录id
     */
    @AuthRequire
    @PostMapping("/get/{id}")
    public ResponseWarp get(@PathVariable Integer id) {
        MarkRecord one = markRecordService.getOne(id);
        if (one != null) {
            return ResponseWarp.ok(one);
        }
        return ResponseWarp.error("获取失败!");
    }

    /**
     * 多条件的分页查询
     * @param map     条件 使用map集合的方式
     *                条件主要包括 pageNo, stage_id, user_id, shop_id, 甚至可以是其他有的字段
     *                这里的前端需要将不必要的条件过滤掉,比如默认查询全部时,请求表单中包涵的空内容就要删除
     */
    @AuthRequire
    @PostMapping("/page")
    public ResponseWarp page(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Integer pageNo = 1;
        if (map.containsKey("pageNo")) {
            pageNo = Integer.parseInt((String) map.remove("pageNo"));
        }
        if (user != null) {  // 添加用户查询条件
            map.put("user_id", user.getId());
        }
        Page<MarkRecord> page = markRecordService.page(map, pageNo);
        if (null != page && page.getContent().size() > 0) {
            return ResponseWarp.ok(page);
        }
        return ResponseWarp.error("查询失败!");
    }
}
